package com.wnhz.work.yz.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.google.gson.Gson;
import com.wnhz.work.yz.BaseActivity;
import com.wnhz.work.yz.MyApplication;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.base.BaseRVAdapter;
import com.wnhz.work.yz.base.BaseViewHolder;
import com.wnhz.work.yz.bean.YaoQingBean;
import com.wnhz.work.yz.utils.Url;
import com.wnhz.work.yz.utils.xutils3.MyCallBack;
import com.wnhz.work.yz.utils.xutils3.XUtil;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class YaoQingActivity extends BaseActivity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_yao_qing);
        initData();
        initTitle();
        initView();
    }

    BaseRVAdapter baseRVAdapter;

    private void initView() {
        Log.e("is", infos.size() + "");
        yaoqing_msg = (RecyclerView) findViewById(R.id.yaoqing_msg);
        yaoqing_msg.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        baseRVAdapter = new BaseRVAdapter(this, infos) {
            @Override
            public int getLayoutId(int viewType) {
                return R.layout.item_yaoqing;
            }

            @Override
            public void onBind(BaseViewHolder holder, final int position) {
                x.image().bind(holder.getImageView(R.id.head_img), infos.get(position).getLogo_img());
                holder.setTextView(R.id.head_name, infos.get(position).getInviter());
                holder.setTextView(R.id.group_name, "邀请你加入\t\t" + infos.get(position).getTeam_name());
                holder.getView(R.id.jujue).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        isJoinTeam(1, position);
                    }
                });
                holder.getView(R.id.tongyi).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        isJoinTeam(2, position);
                    }
                });
            }

        };
        yaoqing_msg.setAdapter(baseRVAdapter);
    }

    private void isJoinTeam(int i, int position) {
        String url = "";
        switch (i) {
            case 1:
                url = Url.TEAM_USERREFUSEDINVITE;
                break;
            case 2:
                url = Url.TEAM_USERAGREEINVITE;
                break;
        }

        Map<String, Object> map = new HashMap<>();
        map.put("token", MyApplication.getInstance().getUserBean().getToken());
        map.put("team_id", infos.get(position).getTeam_id());
        showDialog();

        XUtil.Post(url, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.i("----", "onSuccess:= " + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    String info = jsonObject.optString("info");
                    if ("1".equals(res)) {
                        MyToast(info);
                    } else {
                        if ("-1".equals(res)) {
                            info = "请重新登录";
                        }
                        MyToast(info);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                upData();
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                closeDialog();
            }
        });
    }

    private void upData() {
        Map<String, Object> map = new HashMap<>();
        try {
            map.put("token", MyApplication.getInstance().getUserBean().getToken());
        } catch (Exception e) {
            Intent intent = new Intent(this, LoginNewActivity.class);
            startActivity(intent);
            return;
        }
        XUtil.Post(Url.TEAM_INVITEADDTEAMLIST, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.i("----", "onSuccess:= " + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    String info = jsonObject.optString("info");
                    infos.clear();
                    if ("1".equals(res)) {
                        Gson gson = new Gson();
                        YaoQingBean bean = gson.fromJson(result, YaoQingBean.class);
                        infos = bean.getInfo();
                    } else {
                        if ("-1".equals(res)) {
                            info = "请重新登录";
                        }
                        MyToast(info);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                closeDialog();
                initView();
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                closeDialog();
            }
        });
    }

    private List<YaoQingBean.InfoEntity> infos = new ArrayList<>();
    private RecyclerView yaoqing_msg;

    private void initData() {
        infos = (List<YaoQingBean.InfoEntity>) getIntent().getSerializableExtra("bean");
    }

    private void initTitle() {
        TextView title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText("邀请通知");
        findViewById(R.id.btn_main_left).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_main_left:
                finish();
                break;
        }
    }
}
